package com.android.common.filetools;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;

public class HttpDownloader {

	long device_total, sdcard_total;
	static boolean sdcard_ready = MemoryStatus.externalMemoryAvailable();
	long sdcard_avail = MemoryStatus.getAvailableExternalMemorySize();
	static long device_avail = MemoryStatus.getAvailableInternalMemorySize();

	static long minSize = 1048576;// 内存卡大于10M时可用

	public static String getPath() {

		sdcard_ready = MemoryStatus.externalMemoryAvailable();
		if (sdcard_ready
				&& MemoryStatus.getAvailableExternalMemorySize() > minSize) {
			return MemoryStatus.getExternalDataPath() + "/inspur";
		} else {
			device_avail = MemoryStatus.getAvailableInternalMemorySize();
			if (device_avail > minSize) {
				return MemoryStatus.getInternalDataPath() + "/inspur";
			}
		}
		return null;
	}

	private URL url = null;

	/**
	 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容 1.创建一个URL对象
	 * 2.通过URL对象,创建一个HttpURLConnection对象 3.得到InputStream 4.从InputStream当中读取数据
	 * 
	 * @param dir :目录
	 * @param fileName ： 文件名
	 * @return 1：文件已经存在，-1：文件不存在
	 */
	public int isFileExist(String dir, String fileName) {
		FileUtils fileUtils = new FileUtils();
		if (fileUtils.isFileDirExist(dir)&& fileUtils.isFileExist(dir, fileName)) {
			return 1;
		} else {
			return -1;
		}
	}

	/**
	 * 根据URL下载文件,前提是这个文件当中的内容是文本,函数的返回值就是文本当中的内容 1.创建一个URL对象
	 * 2.通过URL对象,创建一个HttpURLConnection对象 3.得到InputStream 4.从InputStream当中读取数据
	 * 
	 * @param urlStr
	 * @return
	 */
	public String download(String urlStr) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		BufferedReader buffer = null;
		try {
			url = new URL(urlStr);
			HttpURLConnection urlConn = (HttpURLConnection) url
					.openConnection();
			buffer = new BufferedReader(new InputStreamReader(
					urlConn.getInputStream()));
			while ((line = buffer.readLine()) != null) {
				sb.append(line);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				buffer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}

	/**
	 * 从HTTP下载文件到SD存储卡
	 * 
	 * @param urlStr
	 * @param path
	 * @param fileName
	 * @return -1:文件下载出错 0:文件下载成功 1:文件已经存在
	 */
	public int downFile(String urlStr, String path, String fileName) {
		InputStream inputStream = null;
		try {
			FileUtils fileUtils = new FileUtils();

			if (fileUtils.isFileDirExist(path)
					&& fileUtils.isFileExist(path, fileName)) {
				return 1;
			} else {

				inputStream = getInputStreamFromURL(urlStr);

				File resultFile = fileUtils.write2SDFromInput(path, fileName,
						inputStream);
				if (resultFile == null) {
					return -1;
				}
				inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		return 0;
	}

	/**
	 * 从res/raw拷贝文件到SD存储卡
	 * 
	 * @param ctx
	 * @param path
	 *            存放到本地的目录路径
	 * @param fileName
	 *            在本地目录中要生成的文件的名字
	 * @param rawID
	 *            在Project中res/raw下，要拷贝的文件的ID
	 * @return -1:文件下载出错 0:文件下载成功 1:文件已经存在
	 */
	public int downFileFromRaw(Context ctx, String path, String fileName,
			int rawId) {
		InputStream inputStream = null;
		try {
			FileUtils fileUtils = new FileUtils();

			if (fileUtils.isFileDirExist(path)
					&& fileUtils.isFileExist(path, fileName)) {
				return 1;
			} else {

				inputStream = ctx.getResources().openRawResource(rawId);
				File resultFile = fileUtils.write2SDFromInput(path, fileName,
						inputStream);
				if (resultFile == null) {
					return -1;
				}
				inputStream.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		}

		return 0;
	}

	/**
	 * 根据URL得到输入流
	 * 
	 * @param urlStr
	 * @return
	 */
	public InputStream getInputStreamFromURL(String urlStr) {
		HttpURLConnection urlConn = null;
		InputStream inputStream = null;
		try {
			url = new URL(urlStr);
			urlConn = (HttpURLConnection) url.openConnection();
			inputStream = urlConn.getInputStream();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return inputStream;
	}
}
